// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  // 输入时触发的输入框方法
  usernameInput: function(e) {
    //console.log(e.detail.value)
    this.setData({
      userID: e.detail.value
    })
  },

  passwordInput: function(e) {
    //console.log(e.detail.value)
    this.setData({
      password: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  loginAction: function() {
    wx.request({
      url: 'http://localhost:8080/TrainManager/Login',
      data:{
        userID: this.data.userID,
        password: this.data.password,
      },

      success: (res) => {
        console.log(res)
        if(res.data[0] == '1') {

          getApp().globalData.userID = this.data.userID;
          if((res.data[1] == this.data.userID) && (res.data[2] == this.data.password)) {
            wx.showToast({
              title: '登录成功',
            })
            wx.switchTab ({
              url: '/pages/select/select',
            })
          }
          else {
            wx.showToast({
              title: '用户名或密码错误',
              image: '/images/x1.png'
            })
          }
        }
        else {
          wx.showToast({
            title: '登录失败',
            image: '/images/x1.png'
          })
        }
      }
    })
  },

  registerAction: function() {
    wx.navigateTo({
      url: '/pages/register/register',
    })
  }
})